#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""

App 路由注册

@file register.py
@author weiximei
@date 2025/2/17 17:10
"""
from flask import Flask

from app.user.routes import user
from internal import manager
from internal.common import logger
from internal.database import init_db
from internal.errors import YqmError


class AppRegister(Flask):
    env: str = None

    def __init__(self, import_name: str, env: str = ''):
        super().__init__(import_name)
        if env is None or env == '':
            raise YqmError(code=500, message='环境变量 ENV 未设置')

        self.env = env

    # 注册路由
    def register_routes(self) -> Flask:
        # 注册 用户模块
        self.register_blueprint(user, url_prefix='/user')

        return self

    # 注册数据库
    def register_db(self):
        init_db(self)

    # 注册配置管理器
    def register_manager(self):
        manager.init_manager(self.env)
        return self

    # 注册日志
    def register_logger(self):
        logger.setup_logger(self)
        return self
